package com.xbongbong.outerlink.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.outerlink.AbstractProOuterLinkService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.dto.OuterLinkBaseDTO;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.pojo.vo.AfterSaveVO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateGetVO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.formdata.pojo.SaasSpecialParamPojo;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderDataEsListVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.InvoiceRelationshipEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.InvoiceEnum;
import com.xbongbong.saas.help.SaasListHelp;
import com.xbongbong.saas.model.InvoiceRelationshipModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getIntegerOrDefaultFromFormData;

/**
 * 发票业务 外链接口实现
 * @author zheng.li
 **/
@Service
public class InvoiceOuterLinkServiceImpl extends AbstractProOuterLinkService {

    @Resource
    private InvoiceRelationshipModel invoiceRelationshipModel;

    @Resource
    private SaasListHelp saasListHelp;


    @Override
    public List<Integer> businessType() {
        return Collections.singletonList(XbbRefTypeEnum.INVOICE.getCode());
    }

    @Override
    public void handleDataList(FormDataListDTO formDataListDTO, PaasFormDataEsListVO paasFormDataEsListVO, WorkOrderDataEsListVO workOrderDataEsListVO) throws XbbException {
        super.handleDataList(formDataListDTO,paasFormDataEsListVO,workOrderDataEsListVO);
    }

    @Override
    public void handleGetData(OuterLinkBaseDTO outerLinkBaseDTO, PaasFormDataEntityExt formData, ParseSingleRowDataVO parseSingleRowDataVO, List<FieldAttrEntity> explains) throws XbbException {

    }

    @Override
    public void handleGetReturnVo(FormDataUpdateGetVO formDataUpdateGetVO, OuterLinkBaseDTO outerLinkBaseDTO) throws XbbException {
        String corpid = outerLinkBaseDTO.getCorpid();
        Long dataId = outerLinkBaseDTO.getDataId();

        JSONObject result = formDataUpdateGetVO.getData().getData();

        SaasSpecialParamPojo saasSpecialParamPojo = new SaasSpecialParamPojo();
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("invoiceId", outerLinkBaseDTO.getDataId());
        param.put("isCancel", result.getInteger(InvoiceEnum.IS_CANCEL.getAttr()));
        param.put("del", DelEnum.NORMAL.getDel());
        List<InvoiceRelationshipEntity> invoiceRelationshipEntityList = invoiceRelationshipModel.findEntitys(param);

        Integer isRed = getIntegerOrDefaultFromFormData(result,InvoiceEnum.IS_RED.getAttr(), BasicConstant.ZERO);
        JSONArray amountDetail = saasListHelp.getMoneyDetail(invoiceRelationshipEntityList, result, dataId, corpid, 0, BasicConstant.ONE,isRed);
        result.put(PaasConstant.AMOUNTDETAIL, amountDetail);
        if(Objects.equals(isRed,BasicConstant.ONE)) {
            saasSpecialParamPojo.setFormRedEdit(BasicConstant.ONE);
        }

        formDataUpdateGetVO.setSaasSpecialParamPojo(saasSpecialParamPojo);
        super.handleGetReturnVo(formDataUpdateGetVO,outerLinkBaseDTO);
    }


    @Override
    public void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException {
        super.beforeSave(validateDataDTO);
    }


    @Override
    public AfterSaveVO insert(FormDataAddDTO formDataAddDTO, PaasFormDataEntity paasFormDataEntity, PaasFormEntityExt paasFormEntityExt) throws XbbException {
        return super.insert(formDataAddDTO,paasFormDataEntity,paasFormEntityExt);
    }
}
